package com.abss.vote.controller;

import com.abss.vote.model.Administrators;
import com.abss.vote.model.Logo;
import com.abss.vote.model.MpInfo;
import com.abss.vote.model.MyProps;
import com.abss.vote.service.LogoService;
import com.abss.vote.service.UserReceptionService;
import com.abss.vote.util.FileUtils;
import com.abss.vote.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LogoController {
    @Autowired
    private UserReceptionService userReceptionService;

    @Autowired
    private LogoService logoService;
    @Autowired
    private MyProps myProps;

    /**
     * @return
     */
    @RequestMapping("/logo")
    public String getLogo(Model model, HttpSession session){
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        Logo logo = logoService.getLogoByServerId(ad);
        boolean falg = true;
        if(ad.getLEVEL()==1 || ad.getLEVEL()==0){
            falg = true;
        }
        model.addAttribute("falg",falg);
        model.addAttribute("logo",logo);
        return "back/index/logo";
    }

    /**
     * logo图片   将图片保存在此服务器上
     * @param file1
     * @return
     */
    @SuppressWarnings("null")
    @ResponseBody
    @RequestMapping("/LogoUpload")
    public Map<String,Object> picUpload(@RequestParam(value = "file1", required = false) MultipartFile file1){
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Map<String,Object> ret=new HashMap<>();
        String Picture ="";
        String picName ="";
        if (file1.isEmpty()) {
            ret.put("code",0);
            return ret;
        }
        long time1= Utils.getName();//获取当前时间戳
        String fileName1 = file1.getOriginalFilename();//获取原文件名
        Picture = "/copy/"+time1+ fileName1.substring(fileName1.indexOf("."));
        String name = time1+ fileName1.substring(fileName1.indexOf("."));
        File dest1 = new File(mp.getFILE_PATH() + Picture);
        try {
            file1.transferTo(dest1);
            picName = FileUtils.saveLogo(dest1,name,mp);
            ret.put("code",1);
            ret.put("pic",picName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 修改logo
     * @return
     */
    @ResponseBody
    @RequestMapping("/upLogo")
    public Map<String,Object> upLogo(String LOGO_IMG, HttpSession session){
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Map<String,Object> ret=new HashMap<>();
        //获取本服务器的地址
        String img = mp.getDOMAIN_NAME()+LOGO_IMG;//上传的服务器地址
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        String admin = ad.getADMIN_ACCOUNT();
        Logo lo =  new Logo();
        lo.setADMIN(admin);
        lo.setLOGO_IMG(img);
        int res = logoService.updateLogoImg(lo);
        ret.put("code",res);
        return ret;
    }
    /**
     * 修改头像
     * @return
     */
    @ResponseBody
    @RequestMapping("/upFance")
    public Map<String,Object> upFance(String FANCE_IMG, HttpSession session){
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Map<String,Object> ret=new HashMap<>();
        //获取本服务器的地址
        String img = mp.getDOMAIN_NAME()+FANCE_IMG;//上传的服务器地址
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        String admin = ad.getADMIN_ACCOUNT();
        Logo lo =  new Logo();
        lo.setADMIN(admin);
        lo.setFANCE_IMG(img);
        int res = logoService.updateFanceImg(lo);
        ret.put("code",res);
        return ret;
    }

    /**
     * 修改微信
     * @return
     */
    @ResponseBody
    @RequestMapping("/upWx")
    public Map<String,Object> upWx(Logo lo, HttpSession session){
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Map<String,Object> ret=new HashMap<>();
        //获取本服务器的地址
        String img = mp.getDOMAIN_NAME()+lo.getWX_IMG();//上传的服务器地址
        lo.setWX_IMG(img);
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        lo.setADMIN(ad.getADMIN_ACCOUNT());
        int res = logoService.updateWx(lo);
        ret.put("code",res);
        return ret;
    }


    /**
     * 修改管理员头像
     * @return
     */
    @ResponseBody
    @RequestMapping("/upFace")
    public Map<String,Object> upFace(Logo lo, HttpSession session){
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Map<String,Object> ret=new HashMap<>();
        //获取本服务器的地址
        String img = mp.getDOMAIN_NAME()+lo.getFACE_IMG();//上传的服务器地址
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        ad.setFACE_IMG(img);
        int res = logoService.updateFace(ad);
        ret.put("code",res);
        return ret;
    }
}
